package com.example.bpmervice.controller;


import com.example.bpmervice.po.BpmLeave;
import com.example.bpmervice.po.TaskPo;
import com.example.bpmervice.service.impl.BpmLeaveServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bpm/leave")
@RequiredArgsConstructor
@Slf4j
public class BpmLeaveController {

    private final BpmLeaveServiceImpl bpmLeaveServiceImpl;

    @PostMapping("/start")
    public ResponseEntity<BpmLeave> startProcess(@RequestBody BpmLeave leave) {
        return ResponseEntity.ok(bpmLeaveServiceImpl.startLeaveProcess(leave));
    }

    @GetMapping("/tasks/{userId}")
    public ResponseEntity<List<TaskPo>> getTasks(@PathVariable String userId) {
        return ResponseEntity.ok(bpmLeaveServiceImpl.getUserTasks(userId));
    }

    @PostMapping("/complete/{taskId}")
    public ResponseEntity<Void> completeTask(@PathVariable String taskId,
                                             @RequestBody Map<String, Object> variables) {
        bpmLeaveServiceImpl.completeTask(taskId, variables);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/task/{taskId}")
    public ResponseEntity<TaskPo> getTaskById(@PathVariable String taskId) {
        return ResponseEntity.ok(bpmLeaveServiceImpl.getTaskById(taskId));
    }

    @PostMapping("/deploy")
    public ResponseEntity<Void> deployProcess() {
        bpmLeaveServiceImpl.deployProcess();
        return ResponseEntity.ok().build();
    }

    @GetMapping("/taskHistory")
    public ResponseEntity<List<HistoricTaskInstance>> getTaskHistory() {
        return ResponseEntity.ok(bpmLeaveServiceImpl.getTaskHistory());
    }

    @GetMapping("/getFinishInstanceHistory")
    public ResponseEntity<List<HistoricActivityInstance>> getFinishInstanceHistory(String processInstanceId) {
        return ResponseEntity.ok(bpmLeaveServiceImpl.getFinishInstanceHistory(processInstanceId));
    }
}
